// const _Page = require("../../__antmove/component/componentClass.js")("Page");
import * as echarts from "../../ec-canvas/echarts";
const _my = require("../../__antmove/api/index.js")(my);
const wx = _my;
let chart = null;

function initChart(canvas, width, height) {
    chart = echarts.init(canvas, null, {
        width: width,
        height: height
    });
    canvas.setChart(chart);
    var option = {};
    chart.setOption(option);
    return chart;
}


function setChart(opt) {
  var option = opt;
  chart.setOption(option);
  return chart;
}

Page({
    onShareAppMessage: function(res) {
        return {
            title: "ECharts 可以在微信小程序中使用啦！",
            path: "/pages/index/index",
            success: function() {},
            fail: function() {}
        };
    },
    data: {
        ec: {
            // onInit: initChart
        },
        isIde: my.isIDE,
        baseUrl: my.isIDE?'http://127.0.0.1:8001':'https://www.xxrl.asia:8001',
        submitMessage: {
          file: '',     // 上传文件
          name: '',     // 图标名称
          goal: '',     // 分析的目标
          chartType: '折线图' // 图标类型
        },
        chartTypes: [
          { name: '折线图', value: '折线图' , checked: true},
          { name: '柱状图', value: '柱状图' },
          { name: '雷达图', value: '雷达图' },
          // { name: 'vue', value: 'Vue.js' },
          // { name: 'ember', value: 'Ember.js' },
          // { name: 'backbone', value: 'Backbone.js', disabled: true },
        ]
    },

    onInit: function(canvas, width, height) {
      // this.sendMessage();
      initChart(canvas, width, height)
    },

    radioChange(e) {
      console.log('你选择的图表是：', e.detail.value);
      this.data.submitMessage.chartType=e.detail.value;
    },

    setAnalyzeTarget(e) {
      console.log('你的输入目标是：', e.detail.value);
      this.data.submitMessage.goal=e.detail.value
    },

    setChartName(e) {
      console.log('你的输入图标名称是：', e.detail.value);
      this.data.submitMessage.name=e.detail.value
    },

    onLoad: function (options) {
      this.setData({
        src: 'https://ns.s2m.cc/upload?domain=xinghe&maxCount=2'
      })
    },

    uploadFile: function() {
      wx.navigateTo({
        url: src
      });
    },

    getFileURL : function() {
      return WXAPI.channelDataPull('uploadH5Url');
    },

    onSubmit(e) {
      this.genChartByAi();
    },
    onReset() {
      var submitMessage = {
        file: null,
        name: '',
        goal: '',
        chartType: ''
      };
      this.setData({
        submitMessage: submitMessage
      });
      console.log(this.data.submitMessage);
    },

    genChartByAi() {
      console.log("开始发送消息 ~~");
      my.request({
        url: this.data.baseUrl + '/bi/gen',
        method: 'POST',
        data: {
          file: this.data.submitMessage.file,
          name: this.data.submitMessage.name,     // 图标名称
          goal: this.data.submitMessage.goal,     // 分析的目标
          chartType: this.data.submitMessage.chartType // 图标类型
        },
        headers: {
          'content-type': 'application/json', //默认值
        },
        dataType: 'json',
        success: function (res) {          
          // my.alert({ content: 'success' });
        },
        fail: function (error) {
          console.error('fail: ', JSON.stringify(error));
        },
        complete: function (res) {
          my.hideLoading();
          console.log('响应结果：' + res.data.data);
          var opt = res.data.data.genChart;
          setChart(opt);
          // my.alert({ content: 'complete' });
        },
      });
    },

    onReady() {
      // debugger
        setTimeout(function() {
            // 获取 chart 实例的方式
            console.log(chart);
        }, 2000);
    }
});
